// 消耗品弹窗
import { game } from "../../../../script/game/control/game";
import { companyBase } from "../../../../script/game/control/Item/ItemBase";
import { companyType } from "../../../../script/game/gameType";
import { ListViewAdapter } from "../../../../script/util/ListView/ListViewAdapter";
import { ccs } from "../../../../script/util/services";
import { WinBase } from "../../../../script/wins/WinBase";
import companyLine from "../../home/company/companyLine";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends WinBase {

    companyData: companyBase = null;
    company: companyType = null;

     @property(companyLine)
     makeLine: companyLine = null;

     @property(companyLine)
     saleLine: companyLine = null;
    start () {
        //获取事件列表
        this.companyData = game.playerMgr.bag.companyBag.getItem(this._args.id);
        this.company = this.companyData.company;
        this.setInfo();
        this.touchEndEventScale(this.nodes.manager, () => {
            ccs.winMgr.showWin("popWorkerWin", { type: "make", id: this.company.id});
        })
        this.addOperation();
        this.registerEvent("upInfo", this.setInfo);
        this.touchEndEventScale(this.nodes.title, () => {
            ccs.winMgr.showWin('popAlert', {content: "是否花费500w改名字", cb: () => {
                if(!this.companyData.costMoney(5000000)) return;
                ccs.winMgr.showWin('popEditor', {maxLength: 10, cb: (res) => {
                    this.company.name = res;
                    this.nodes.title.getComponent(cc.Label).string = res;
                }}, )
            }})
            
        })
    }
    setInfo(){
        this.nodes.title.getComponent(cc.Label).string = this.company.name;
        this.nodes.time.getComponent(cc.Label).string = this.company.year + "";
        if(this.company.ceo) {
            this.nodes.ceo.getComponent(cc.Label).string = "总经理" + (this.company.ceo.name);
        } else {
            this.nodes.ceo.getComponent(cc.Label).string = "总经理：无";
        }
        this.nodes.comWorkers.getComponent(cc.Label).string = `员工：${ccs.gf.getOmitNum(this.company.workers)}/${ccs.gf.getOmitNum(this.company.maxWorker)}`;
        this.nodes.money.getComponent(cc.Label).string = `资金：${ccs.gf.getOmitNum(this.company.money)}`;
        this.nodes.monthIncome.getComponent(cc.Label).string = `月收益：${ccs.gf.getOmitNum(this.company.incomeMonth)}`;
        this.nodes.rent.getComponent(cc.Label).string = `租金：${ccs.gf.getOmitNum(this.company.rent)}`;
        this.nodes.level.getComponent(cc.Label).string = `等级：${ccs.gf.getOmitNum(this.company.level)}`;
        this.nodes.wage.getComponent(cc.Label).string = `薪资：${ccs.gf.getOmitNum(this.company.pay)}`;
        this.nodes.monthPay.getComponent(cc.Label).string = `月支出：${ccs.gf.getOmitNum(this.company.payMonth)}`;
        if(this.company.salesType.count > 0) {
            this.company.salesType.state = 1;
        } else {
            this.company.salesType.state = 0;
        }
        if(this.company.makeType.count > 0) {
            this.company.makeType.state = 1;
        } else {
            this.company.makeType.state = 0;
        }
        this.companyData.calMake();
        this.companyData.calSales();
        this.makeLine.setData(this.company.makeType, this.company.id, "make");
        this.saleLine.setData(this.company.salesType, this.company.id, "sales");

    }
    addOperation() {
        this.touchEndEventScale(this.nodes.ceoMgr, () => {
            ccs.winMgr.showWin("popHireCeoWin", {id: this._args.id});
        })
        this.touchEndEventScale(this.nodes.moneyMgr, () => {
            ccs.winMgr.showWin("popCompanyMoneyWin", {id: this._args.id});
        })
        this.touchEndEventScale(this.nodes.workerMgr, () => {
            ccs.winMgr.showWin("popCompanyWorkerWin", {id: this._args.id});
        })
        this.touchEndEventScale(this.nodes.expand, () => {
            let cost = this.company.level * this.company.levelCost;
            let works = this.company.addWorker * this.company.level;
            let rent = this.company.rent/this.company.level;
            ccs.winMgr.showWin("popAlert", {
                title: "公司扩大",
                content: `扩张公司需要${ccs.gf.getOmitNum(cost)}元，增加员工${ccs.gf.getOmitNum(works)},租金增加${ccs.gf.getOmitNum(rent)},确定扩张吗?`,
                cb: () => {
                    if(!this.companyData.costMoney(cost)) {
                        return ccs.winMgr.showWin("toast", {msg: "公司资金紧张，无法扩张"});
                    }
                    this.company.level++;
                    this.companyData.company.maxWorker += works;
                    this.companyData.company.rent += rent;
                    ccs.winMgr.showWin("toast", {msg: "升级成功"});
                    ccs.mainEmitter.emit("upInfo");
                }
            });
        })
        this.touchEndEventScale(this.nodes.saleBtn, () => {
            if(this.company.money < 0) {
                ccs.winMgr.showWin("toast", {msg: "公司有贷款未偿还，无法出售，请选择破产"});
                return 
            }
            
            let money = this.company.level * this.company.levelCost + this.company.workers * this.company.wage * 0.5;
            money += this.company.money;
            ccs.winMgr.showWin("popAlert", {
                title: "公司出售",
                content: `出售公司将获得${ccs.gf.getOmitNum(money)}元，确定出售吗？`,
                cb: () => {
                    ccs.winMgr.showWin("toast", {msg: `出售成功，进账${ccs.gf.getOmitNum(money * 0.9)}元（扣除10%的税收）`});
                    game.playerMgr.calMoney(money * 0.9);
                    game.playerMgr.bag.companyBag.delItem(this.company.id);
                    let companyIdx = game.playerMgr.heroInfo.companyList.findIndex(val => val.id == this.company.id);
                    game.playerMgr.heroInfo.companyList[companyIdx] = ccs.dataMgr.getCompanyById(this.company.id);
                    ccs.mainEmitter.emit('upCompany');
                    this._close();
                }
            });
        })
        this.touchEndEventScale(this.nodes.broke, () => {
            let money = 0;
            let msg = '';
            if(this.company.money < 0) {
                if(Math.abs(this.company.money) > this.company.inverst) {
                    money = -this.company.inverst;
                } else {
                    money = this.company.money;
                }
                msg = `成功申请破产保护，剩余欠款${ccs.gf.getOmitNum(Math.abs(money))}元`;
            } else {
                money = this.company.money;
                msg = `申请破产获得账户上的资金${ccs.gf.getOmitNum(Math.abs(money))}元`
            }
            ccs.winMgr.showWin("popAlert", {
                title: "公司破产",
                content: `${msg}，并且不得再进行有关行业的商业行为，确定吗？`,
                cb: () => {
                    ccs.winMgr.showWin("toast", {msg: `成功申请破产`});
                    game.playerMgr.heroInfo.money += money;
                    game.playerMgr.bag.companyBag.delItem(this.company.id);
                    let companyIdx = game.playerMgr.heroInfo.companyList.findIndex(val => val.id == this.company.id);
                    game.playerMgr.heroInfo.companyList[companyIdx] = ccs.dataMgr.getCompanyById(this.company.id);
                    ccs.mainEmitter.emit('upCompany');
                    ccs.mainEmitter.emit('updateAttribute');
                    this.company.isLock = 1;
                    this._close();
                }
            });
        })
    }

}   
